package com.clbs.ex.springgwt.server.dao.impl;

import com.clbs.ex.springgwt.client.model.Issue;
import com.clbs.ex.springgwt.server.dao.IssueDAO;
import com.clbs.ex.springgwt.server.mapper.IssueMapper;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 *
 * @author peerawat.poombua
 */
public class IssueDAOImpl extends JdbcDaoSupport implements IssueDAO {

    public void createIssue(Issue issue) {
        String INSERT_ISSUE = "INSERT INTO tblIssue (cName, cDescription, nIssueStatusID)"
                + " values (?,?,?)";
        getJdbcTemplate().update(INSERT_ISSUE
                , new Object[] {issue.getName()
                        , issue.getDescription()
                        , issue.getIssue_statusID()});
    }

    public Issue getIssue(int issueID) {
        String SELECT_ISSUE = "SELECT * FROM tblIssue WHERE nIssueID = ?";
        List<Issue> matched = getJdbcTemplate().query(
                SELECT_ISSUE, new Object[]{issueID}, new IssueMapper());
        return matched.size() > 0 ? matched.get(0) : null;
    }

    public boolean deleteIssue(int issueID) {
        String DELETE_ISSUE = "DELETE FROM tblIssue WHERE nIssueID = ?";
        try {
             getJdbcTemplate().update(DELETE_ISSUE, new Object[]{issueID});
        } catch (DataAccessException e) {
            return false;
        }
        return true;
    }

    public List<Issue> getAllIssues() {
        String GET_ALL_ISSUES = "SELECT * FROM tblIssue";
        List<Issue> matched = getJdbcTemplate().query(GET_ALL_ISSUES, new IssueMapper());
        return matched.size() > 0 ? matched : null;
    }
}
